<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script src="./js/jquery-3.6.0.min.js"></script>
    <script>
           function getById() {
            var url = document.location.toString();
            //分隔开连接,用?把传的值取出来
            var arrObj = url.split("?");
            var id = arrObj[1].split("id");
            return id[1];

        }
         $(function(){
            let id = getById();
            $.get(`http://localhost:3000/blogs/`+id,function(res){
                console.log(res);
                let body = $("body")
                let html = `
                <table>
        <tr>
            <td>标题</td>  
            <td><input type="text" id="title" value="${res[0].title}"></td>
        </tr>
        <tr>
            <td>摘要</td>  
            <td><input type="text" id="abstract " value="${res[0].abstract}"></td>
        </tr>
        <tr>
            <td>内容</td>  
            <td><input type="text" id="content"  value="${res[0].content}"></td>
        </tr>
        <tr>
            <td>分类</td>  
            <td><input type="text" id="classify"  value="${res[0].classify}"></td>
        </tr>

        <tr>
            <td>作者</td>  
            <td><input type="text" id="author"  value="${res[0].author}"></td>
            
        </tr>
    </table>
    <input type="button" value="保存" onclick="update(${res[0].id})">
    <input type="button" value="返回" onclick="cancel()">
         `
                body.append(html)       
            })
        })
        function cancel(){
            location.href="./index.html"
        }
        function update(id){
        //更新。获得值之后更新
        let title = $("#title").val();
        let abstract = $("#abstract").val();
        let content = $("#content").val();
        let classify = $("#classify").val();
        let author = $("#author").val();
            let obj = {
               id, title,abstract,content,classify,author
                }
                $.ajax({
                    url: 'http://localhost:3000/blogs/id',
                    type: 'put',
                    data: obj,
                    success: function (res) {
                        alert("修改成功")
                        window.location.href = "./index.html"
                    }
                })
        }
    </script>
</body>
</html>